package cn.com.huzo.service.trade.impl;

import cn.com.huzo.model.entities.trade.ProductionInstructionLinesDestinationEntity;
import cn.com.huzo.repository.trade.ProductionInstructionLinesDestinationRepository;
import cn.com.huzo.repository.trade.ProductionInstructionLinesDestinationSchemaRepository;
import cn.com.huzo.repository.trade.ProductionInstructionLinesRepository;
import cn.com.huzo.service.trade.ProductionInstructionLinesDestinationManager;
import huzofw.service.impl.GenericManagerImpl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 生产指示书明细-发货目的地数据管理服务对象类实现。
 *
 * @author 吴旭
 */
@Service("ProductionInstructionLinesDestinationManager")
public class ProductionInstructionLinesDestinationManagerImpl
        extends GenericManagerImpl<ProductionInstructionLinesDestinationEntity, String>
        implements ProductionInstructionLinesDestinationManager {

    @Resource(name = "ProductionInstructionLinesRepository")
    private ProductionInstructionLinesRepository _dao_lines;

    @Resource(name = "ProductionInstructionLinesDestinationRepository")
    private ProductionInstructionLinesDestinationRepository _dao_lines_destination;

    @Resource(name = "ProductionInstructionLinesDestinationSchemaRepository")
    private ProductionInstructionLinesDestinationSchemaRepository _dao_lines_schedule;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public List<ProductionInstructionLinesDestinationEntity> fetchByHeaderId(Long headerId) throws Exception {
        return _dao_lines_destination.fetchProductionInstructionLinesDestinationNode(headerId);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Override
    @Transactional
    public Map<String, Object> updateDestinationOrder(List<ProductionInstructionLinesDestinationEntity> itemList) throws Exception {
        Map<String, Object> _result = new HashMap();

        Integer rowsAffected = _dao_lines_destination.updateDestinationOrder(itemList);

        if (rowsAffected > 0) {
            _result.put("Success", true);
            _result.put("DestinationNode", _dao_lines_destination.fetchProductionInstructionLinesDestinationNode(itemList.get(0).getHeaderId()));
            _result.put("LinesList", _dao_lines.fetchLinesByHeaderIdAndStyleNo(itemList.get(0).getHeaderId(), null));
        } else {
            _result.put("Success", false);
        }

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Override
    @Transactional
    public Map<String, Object> save(Map<String, ? extends List<ProductionInstructionLinesDestinationEntity>> criterion) throws Exception {
        Map<String, Object> _result = new HashMap();
        int _rowsAffected1 = 0;
        int _rowsAffected2 = 0;
        Long _headerId = 0L;
        if (criterion.get("insertList").size() != 0) {
            _rowsAffected1 = _dao_lines_destination.batchInsert(criterion.get("insertList"));
            _headerId = criterion.get("insertList").get(0).getHeaderId();
            for (int i = 0; i < criterion.get("insertList").size(); i++) {
                Map _params = new HashMap();
                _params.put("HeaderId", criterion.get("insertList").get(i).getHeaderId());
                _params.put("Destination", criterion.get("insertList").get(i).getDestination());
                _params.put("CreatedBy", criterion.get("insertList").get(i).getCreatedBy());
                _params.put("UpdatedBy", criterion.get("insertList").get(i).getUpdatedBy());

                _dao_lines_schedule.insertDestination(_params);
            }
            _dao_lines_schedule.deleteByNullDestination(_headerId);
        }
        if (criterion.get("updateList").size() != 0) {
            _rowsAffected2 = _dao_lines_destination.batchUpdate(criterion.get("updateList"));
            _headerId = criterion.get("updateList").get(0).getHeaderId();
        }

        if ((_rowsAffected1 + _rowsAffected2) > 0) {
            _result.put("Success", true);
            _result.put("DestinationNode", _dao_lines_destination.fetchProductionInstructionLinesDestinationNode(_headerId));
            _result.put("LinesList", _dao_lines.fetchLinesByHeaderIdAndStyleNo(_headerId, null));
        } else {
            _result.put("Success", false);
        }
        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Override
    @Transactional
    public Map<String, Object> delete(Map<String, Object> criterion) throws Exception {
        Map<String, Object> _result = new HashMap();

        Integer rowsAffected = _dao_lines_destination.delete(MapUtils.getString(criterion, "LinesId"));
        if (rowsAffected > 0) {
            _result.put("Success", true);
            _result.put("DestinationNode", _dao_lines_destination.fetchProductionInstructionLinesDestinationNode(MapUtils.getLong(criterion, "HeaderId")));
            _result.put("LinesList", _dao_lines.fetchLinesByHeaderIdAndStyleNo(MapUtils.getLong(criterion, "HeaderId"), null));
        } else {
            _result.put("Success", false);
        }

        return _result;
    }
}
